package com.jeepig.evaluation.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.jeepig.common.utils.DateUtils;
import com.jeepig.common.utils.SecurityUtils;
import com.jeepig.common.utils.StringUtils;
import com.jeepig.evaluation.domain.Evaluate;
import com.jeepig.evaluation.domain.EvaluateItem;
import com.jeepig.evaluation.mapper.EvaluateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeepig.evaluation.mapper.SelfEvaluationMapper;
import com.jeepig.evaluation.domain.SelfEvaluation;
import com.jeepig.evaluation.service.ISelfEvaluationService;

/**
 * 自我评价结果Service业务层处理
 *
 * @author guoxb
 * @date 2025-11-01
 */
@Service
public class SelfEvaluationServiceImpl implements ISelfEvaluationService
{
    @Autowired
    private SelfEvaluationMapper selfEvaluationMapper;

    @Autowired
    private EvaluateMapper evaluateMapper;

    /**
     * 查询自我评价结果
     *
     * @param id 自我评价结果主键
     * @return 自我评价结果
     */
    @Override
    public SelfEvaluation selectSelfEvaluationById(String id)
    {
        return selfEvaluationMapper.selectSelfEvaluationById(id);
    }

    /**
     * 查询自我评价结果列表
     *
     * @param selfEvaluation 自我评价结果
     * @return 自我评价结果
     */
    @Override
    public List<SelfEvaluation> selectSelfEvaluationList(SelfEvaluation selfEvaluation)
    {
        return selfEvaluationMapper.selectSelfEvaluationList(selfEvaluation);
    }

    @Override
    public List<SelfEvaluation> selectSelfEvaluationByMemberId(Long memberId) {
        List<SelfEvaluation> selfEvaluations = selfEvaluationMapper.selectSelfEvaluationByMemberId(memberId);
        if(StringUtils.isEmpty(selfEvaluations)){
            //构造自评价项目
            List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(new Evaluate());
            selfEvaluations = new ArrayList<>();
            if(StringUtils.isNotEmpty(evaluates)){
                for(Evaluate evaluate : evaluates){
                    SelfEvaluation selfEvaluation = new SelfEvaluation();
                    selfEvaluation.setMemberId(memberId);
                    selfEvaluation.setSn(evaluate.getSn());
                    selfEvaluation.setEvalItemId(evaluate.getId());
                    selfEvaluation.setEvalItemName(evaluate.getName());
                    selfEvaluations.add(selfEvaluation);
                }
            }

        }
        return selfEvaluations;
    }

    @Override
    public SelfEvaluation selectSelfEvaluationByMemberId(Long memberId, Long evaluateId) {
        SelfEvaluation selfEvaluation = new SelfEvaluation();
        selfEvaluation.setMemberId(memberId);
        selfEvaluation.setEvalItemId(evaluateId);
        List<SelfEvaluation> selfEvaluations = selfEvaluationMapper.selectSelfEvaluationList(selfEvaluation);
        return StringUtils.isNotEmpty(selfEvaluations)? selfEvaluations.get(0): null;
    }

    /**
     * 新增自我评价结果
     *
     * @param selfEvaluation 自我评价结果
     * @return 结果
     */
    @Override
    public int insertSelfEvaluation(SelfEvaluation selfEvaluation)
    {
        selfEvaluation.setCreateBy(SecurityUtils.getUsername());
        selfEvaluation.setCreateTime(DateUtils.getNowDate());
        return selfEvaluationMapper.insertSelfEvaluation(selfEvaluation);
    }

    /**
     * 修改自我评价结果
     *
     * @param selfEvaluation 自我评价结果
     * @return 结果
     */
    @Override
    public int updateSelfEvaluation(SelfEvaluation selfEvaluation)
    {
        selfEvaluation.setUpdateBy(SecurityUtils.getUsername());
        selfEvaluation.setUpdateTime(DateUtils.getNowDate());
        return selfEvaluationMapper.updateSelfEvaluation(selfEvaluation);
    }

    /**
     * 批量删除自我评价结果
     *
     * @param ids 需要删除的自我评价结果主键
     * @return 结果
     */
    @Override
    public int deleteSelfEvaluationByIds(String[] ids)
    {
        return selfEvaluationMapper.deleteSelfEvaluationByIds(ids);
    }

    @Override
    public int deleteSelfEvaluationByMemberId(Long memberId) {
        return selfEvaluationMapper.deleteSelfEvaluationByMemberId(memberId);
    }

    /**
     * 删除自我评价结果信息
     *
     * @param id 自我评价结果主键
     * @return 结果
     */
    @Override
    public int deleteSelfEvaluationById(String id)
    {
        return selfEvaluationMapper.deleteSelfEvaluationById(id);
    }
}
